package com.hua.designpattern.creational.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class CloneByIO implements Serializable {
	private static final long serialVersionUID = 4939062736734143752L;
	private int id = 1;
	private Integer age = new Integer(2);
	private String name = new String("3");
	private PersonByIO person = new PersonByIO();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PersonByIO getPerson() {
		return person;
	}

	public void setPerson(PersonByIO person) {
		this.person = person;
	}

	@Override
	public String toString() {
		return super.toString() + " [id=" + id + " ;age=" + age + "; name=" + name + " ;person=" + person + "]";
	}

	public CloneByIO deepClone() {

		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(this);

			ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
			ObjectInputStream ois = new ObjectInputStream(bis);
			return (CloneByIO) ois.readObject();

		} catch (IOException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
